// 提交售后订单
import { PACKAGE_ORDER_PAGE } from '../../../constants/route';
import { ORDER_AFTER_SALE_TYPE, GOODS_STATUS_TYPE, ORDER_REFUND_REASON, ORDER_SALES_RETURN_REASON, PRODUCT_RECEIVING_STATUS } from '../../../constants/order';
import { MESSAGE_ID } from '../../../constants/message';
import { redirectTo, tipToast, subscribeMessage } from '../../../common/env';
import { getAfterSaleOrderInfo } from '../../../biztools/wex';
import { createAfterSaleOrder } from '../../api/order';

Page({
    data: {
        orderInfo: {},
        isOnlyRefund: false,
        goodsId: '',
        goodsInfo: {},
        goodsPrice: 0,
        count: 1,
        status: '',
        reason: '',
        remark: '',
        statusList: GOODS_STATUS_TYPE,
        reasonList: ORDER_REFUND_REASON,
        showStatusModal: false,
        showReasonModal: false,
        showRemarkModal: false,
    },
    onLoad: function (options) {
        const { type, goodsId } = options;
        const orderInfo = getAfterSaleOrderInfo();
        const isOnlyRefund = `${type}` === ORDER_AFTER_SALE_TYPE.onlyRefund;
        const goodsInfo = (orderInfo.details || []).filter(item => `${item.orderDetailId}` === `${goodsId}`)[0];
        this.setData({
            goodsInfo,
            isOnlyRefund,
            orderInfo,
            goodsId,
            status: isOnlyRefund ? '' : { value: PRODUCT_RECEIVING_STATUS.yes },
            goodsPrice: goodsInfo.price,
            count: goodsInfo.quantity,
            reasonList: isOnlyRefund ? ORDER_REFUND_REASON : ORDER_SALES_RETURN_REASON,
        });
    },
    // 打开选择商品状态
    openStatusModal: function () {
        this.setData({
            showStatusModal: true,
        });
    },
    // 打开选择原因
    openReasonModal: function () {
        this.setData({
            showReasonModal: true,
        });
    },
    // 打开修改备注
    openRemarkModal: function () {
        this.setData({
            showRemarkModal: true,
        });
    },
    // 修改数量
    changeCount: function (event) {
        this.setData({
            count: event.detail,
        });
    },
    // 修改商品状态
    changeStatus: function (event) {
        this.setData({
            status: event.detail.value,
        });
    },
    // 修改原因
    changeReason: function (event) {
        this.setData({
            reason: event.detail.value,
        });
    },
    // 修改备注
    changeRemark: function (event) {
        this.setData({
            remark: event.detail,
        });
    },
    dealParam: function () {
        const { status, reason, isOnlyRefund } = this.data;
        if (isOnlyRefund) {
            if (!status) {
                tipToast('商品状态不能为空');
                return false;
            }
        }
        if (!reason) {
            tipToast(isOnlyRefund ? '退款理由不能为空' : '退货理由不能为空');
            return false;
        }
        return true;
    },
    // 提交申请
    applyAction: async function () {
        if (!this.dealParam()) return;
        const { status, goodsInfo, reason, remark, isOnlyRefund, orderInfo, count } = this.data;
        const details = [{
            orderDetailId: goodsInfo.orderDetailId,
            quantity: count,
            goodsReceivingStatus: status.value,
        }]
        const params = {
            orderSerialNumber: orderInfo.serialNumber,
            type: isOnlyRefund ? ORDER_AFTER_SALE_TYPE.onlyRefund : ORDER_AFTER_SALE_TYPE.refundAndSale,
            afterSaleReason: reason,
            remark,
            details,
        };
        const res = await createAfterSaleOrder(params);
        if (res) {
            await subscribeMessage([MESSAGE_ID.order_aftersale]);
            redirectTo(PACKAGE_ORDER_PAGE.orderAfterSaleDetail, {
                orderNumber: res.serialNumber,
            });
        }
    },
})